from django.http import JsonResponse
from django.shortcuts import render, get_object_or_404

from main.models import *
from operate.models import Collect
from user.models import CustomUser


# Create your views here.


def collect(request):
    q_id = request.POST.get("question")
    u_id = request.POST.get("user")

    user = get_object_or_404(CustomUser, id=u_id)
    question = get_object_or_404(Question, id=q_id)

    collect = Collect.objects.filter(user=user, question=question).first()
    if collect is None:
        Collect.objects.create(user=user, question=question)
        return JsonResponse({'code': 0, 'status': 'success', 'msg': "收藏成功"})

    else:
        collect.delete()
        return JsonResponse({'code': 0, 'status': 'failed', 'msg': "取消收藏成功"})